import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterprovider/bean/user_bean.dart';
import 'package:flutterprovider/service/user.dart';
import 'package:provider/provider.dart';

class Login extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<UserModel>(
      create: (_) => UserModel(Provider.of(context)),
      child: Column(
        children: <Widget>[
          Text(
              "姓名:${Provider.of<Userbean>(context).name} \n 地址:${Provider.of<Userbean>(context).address} \n 年龄:${Provider.of<Userbean>(context).age}  "),
          Consumer<UserModel>(
            builder: (_, model, child) {
              if (model.loadding) {
                return CupertinoActivityIndicator();
              } else {
                return MaterialButton(
                  onPressed: () async {
                    print("点击了登录");
                    Userbean result = await model.login("123333");
                    Userbean proBean =
                        Provider.of<Userbean>(context, listen: false);
                    proBean.update(result.name, result.address, result.age);
                  },
                  color: Colors.blueAccent,
                  textColor: Colors.white,
                  child: Text("登录"),
                );
              }
            },
          ),
        ],
      ),
    );
  }
}
